package client;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.File;

public class Client_UI extends JPanel
        implements ActionListener, Runnable {

    JButton go, logout;
    JTextArea WD;
    String Cur = "";
    String Cur2 = "";
    JFileChooser chooser;
    String choosertitle;

    public Client_UI() {
        go = new JButton("Choose Working Directory");
        go.setName("go");
        go.addActionListener(this);

        add(go);

        logout = new JButton("Logout");
        logout.setName("logout");
        logout.addActionListener(this);
        add(logout);

        WD = new JTextArea(Cur);
        add(WD);
    }

        public String[] listFileFolder(String workingdirectory) {
        File folder = new File(workingdirectory);
        File[] listOfFiles = folder.listFiles();
        String[] list = new String[listOfFiles.length];

        for (int i = 0; i < listOfFiles.length; i++) {
            list[i] = listOfFiles[i].getName();

            if (listOfFiles[i].isFile()) {
                System.out.println("File :" + listOfFiles[i].getName());
            } else if (listOfFiles[i].isDirectory()) {
                System.out.println("Directory :" + listOfFiles[i].getName());
            }
        }
        return list;
    }

    public void actionPerformed(ActionEvent e) {
                JButton b = (JButton) e.getSource();
        String buttonname = b.getName();
        if (buttonname.equals("go")) {
        int result;

        chooser = new JFileChooser();
        chooser.setCurrentDirectory(new java.io.File(""));
        chooser.setDialogTitle(choosertitle);
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        //
        // disable the "All files" option.
        //
        chooser.setAcceptAllFileFilterUsed(false);
        //
        if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            System.out.println("getCurrentDirectory(): "
                    + chooser.getCurrentDirectory());
            System.out.println("getSelectedFile() : "
                    + chooser.getSelectedFile());

            Cur = chooser.getCurrentDirectory().toString();
            WD.setText(Cur);

            Cur2 = chooser.getSelectedFile().toString();


        listFileFolder(Cur2);

        } else {
            System.out.println("No Selection ");
        }
        }
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(500, 100);
    }

    public static void main(String s[]) {
        JFrame frame = new JFrame("");
        Client_UI panel = new Client_UI();
        frame.addWindowListener(
                new WindowAdapter() {

                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                    }
                });
        frame.getContentPane().add(panel, "Center");
        frame.setSize(panel.getPreferredSize());
        frame.setVisible(true);


    }

    public void run() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
